package heap;

/**
 * 数据流中第K大的元素 - 703
 *
 * @author yyz
 * @create 2024-01-26 11:19
 */
public class KthLargest {
    MinHeap minHeap;

    public KthLargest(int k, int[] nums) {
        this.minHeap = new MinHeap(k);
        for (int num : nums) {
            add(num);
        }
    }

    public int add(int val) {
        if (!minHeap.isFull()) {
            minHeap.offer(val);
        } else if (minHeap.peek() < val) {
            minHeap.replace(val);
        }
        return minHeap.peek();
    }

    public static void main(String[] args) {
        KthLargest test = new KthLargest(3,
                new int[]{});

        System.out.println(test.add(3)); // [3] 3
        System.out.println(test.add(5)); // [3 5] 3
        System.out.println(test.add(10));
        System.out.println(test.add(9));
        System.out.println(test.add(4));
    }
}
